package demo01;

//除自身以外数组的乘积
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        //除自身以外前元素的积
        int[] f = new int[n];
        f[0] = 1;
        for(int i = 1;i < n;i++){
            f[i] = f[i-1] * nums[i-1];
        }
        //除自身以外后元素的积
        int[] g = new int[n];
        g[n-1] = 1;
        for(int j = n-2;j >= 0;j--){
            g[j] = g[j+1] * nums[j+1];
        }
        int[] m = new int[n];
        for(int k = 0;k < n;k++){
            m[k] = g[k]*f[k];
        }
        return m;
    }
}